import requests
from lxml import etree
from urllib import request
import os
import re
import time


def mkdir(path):
    # 创建本地路径
    if not os.path.exists(path):
        os.makedirs(path)
        print(path, "已创建")
    else:
    	return

def get_source(url):
	headers = {
		"User-Agent" : "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36",
	    "Cookie" : "UNRt_2132_saltkey=Ci3m50kd; UNRt_2132_lastvisit=1548205336; __51cke__=; UNRt_2132_ulastactivity=1548210357%7C0; UNRt_2132_auth=380fxTsPvf9346jQ1aWh8dkfmukCxTG1o%2FgAbfadcN1PmpKD%2Baf0y%2Bq%2FmnzSOJEz9JC1i6Y%2FWQXqaMP3jrELRI9FR2jT; UNRt_2132_lastcheckfeed=1084124%7C1548210357; UNRt_2132_lip=122.224.206.179%2C1548210357; UNRt_2132_st_p=1084124%7C1548210360%7C7e1e14d00050a2b7e8ebe245a1bd2d17; UNRt_2132_visitedfid=21; UNRt_2132_viewid=tid_3088470; UNRt_2132_sendmail=1; UNRt_2132_lastact=1548210361%09home.php%09spacecp; __tins__1987537=%7B%22sid%22%3A%201548208938584%2C%20%22vd%22%3A%202%2C%20%22expires%22%3A%201548212163521%7D; __51laig__=2; UNRt_2132_noticeTitle=1; UNRt_2132_smile=1D1"
		}
	resp = requests.get(url,headers=headers)
	return resp.text

def get_picture(resp):
	source = etree.HTML(resp)
	tables = source.xpath('//table[@class="plhin"]')[1:]
	for table in tables:
		try:
			html = etree.tostring(table, encoding='utf-8').decode('utf-8')
			# with open('df.html', 'w', encoding='utf8') as f:
			# 	f.write(html)
			# break
			pattern = re.compile('<i class="pstatus">[\s\S]*?</i><br />[\s\S]*?<br />([\s\S]*?)<br />')
			city_name = re.findall(pattern, html)[0]
			city_name = (city_name.strip().replace('&nbsp', '')).replace(';', ' ')
			base_dir = 'E:/gaoloumiPic'
			filename = base_dir + '/' + city_name + '/'
			mkdir(filename)
			pattern = re.compile('<div class="mbn">[\s\S]*?file="([\s\S]*?)"')
			city_pics = re.findall(pattern, html)
			for city_pic in city_pics:
				try:
					request.urlretrieve(city_pic, filename+str(time.time())+'.jpg')
				except:
					pass
			print('%s图片已经下载完成' % city_name)
		except:
			print(city_name,'下载失败')

def main():
	for i in range(1,4):
		url = 'http://gaoloumi.cc/forum.php?mod=viewthread&tid=3088470&extra=&page='+ str(i)
		resp = get_source(url)
		get_picture(resp)
		print('下载失败')




if __name__ == '__main__':
	main()
